Trò chơi đua xe động vật trong UNITY Engine
114.795 lượt xem;
- ShopScreen.cs
- ShopScreen /
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3 using Dialog;
4
5 public class ShopScreen : InputAdapter {
6
7 public GameObject bgObject;
8 public BitmapFont nameFont;
9
10 public AnimalNameShop animalName;
11 public GameObject lockObject;
12 public GameObject nextButton;
13
14 private string[] animalNames;
15 //private int animalUnlock;
16
17 public PickLayer pickLayer;
18
19 private BitmapFont shopFont;
20 public Label starLabel;
21 public Label goldLabel;
22
23 public UpgradeLayer upgradeLayer;
24 public BuyLayer buyLayer;
25
26 private int[] starCosts;
27 private int[] goldCosts;
28
29 private UpgradeInfo upgradeInfo;
30
31 private int totalStar;
32
33 public GameObject dialog;
34
35 public void Awake()
36 {
37 nameFont = new BitmapFont("Fonts/bitmapfont1", "Fonts/bitmapfont1_xml", gameObject);
38 shopFont = new BitmapFont("Fonts/shop_font","Fonts/shop_font_xml", null);
39 starCosts = new int[] {0, 15, 25, 35, 45, 60, 75, 90, 115, 135 };
40 goldCosts = new int[] {0, 3000, 5000, 7000, 9000, 12000, 15000, 18000, 22000, 26000};
41
42 upgradeInfo = new UpgradeInfo();
43 }
44
45 public void Start()
46 {
47 InputController.Name = InputNames.SHOP;
48
49 bgObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Textures/bg" + (Attr.currentWorld + 1));
50
51 animalNames = new string[] { "dog", "monkey", "pig", "fox", "giraffe", "panda", "rhino", "tiger", "elephant", "lion" };
52 animalName.setFont(nameFont);
53 //animalUnlock = Data.getData(Data.KEY_ANIMAL_UNLOCK);
54
55 updateAnimalName();
56
57 starLabel.setFont(shopFont);
58 totalStar = 0;
59 for (int i = 0; i < 60; i++)
60 {
61 totalStar += Data.getData(Data.KEY_STAR + i);
62 }
63 starLabel.setText("" + totalStar, 0, 0);
64 goldLabel.setFont(shopFont);
65 UpdateCoin();
66
67 upgradeLayer.setFont(shopFont);
68 upgradeLayer.setUpgradeInfo(upgradeInfo);
69 upgradeLayer.UpdateUI(1);
70 upgradeLayer.UpdateUI(0);
71
72 buyLayer.setFont(shopFont);
73 updateUI();
74
75 {
76 dialog = (GameObject)Instantiate(dialog);
77 DialogUnity dialogUnity = dialog.GetComponent<DialogUnity>();
78 //dialogUnity.setText("Do you want to play", "game without skills?");
79 dialogUnity.setDialogOne(delegate() {
80 InputController.Name = InputNames.SHOP;
81 });
82 }
83
84 ARController.setBannerVisible(false);
85 ARController.showInterstitialAd();
86 }
87
88 public void LateUpdate()
89 {
90 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Backspace))
91 {
92 if (InputController.Name == InputNames.DIALOG)
93 {
94 if (dialog != null)
95 {
96 dialog.GetComponent<DialogUnity>().hideDialog();
97 InputController.Name = InputNames.SHOP;
98 }
99 }
100 }
101 }
102
103 public void updateAnimalName()
104 {
105 animalName.setName(animalNames[Attr.currentAnimal].ToUpper());
106 if (Data.getData(Data.KEY_ANIMAL_UNLOCK + Attr.currentAnimal) == 1)
107 {
108 lockObject.SetActive(false);
109 nextButton.SetActive(true);
110 upgradeLayer.gameObject.SetActive(true);
111
112 upgradeLayer.UpdateUI(1);
113 upgradeLayer.UpdateUI(0);
114
115 buyLayer.gameObject.SetActive(false);
116 }
117 else
118 {
119 lockObject.SetActive(true);
120 nextButton.SetActive(false);
121 upgradeLayer.gameObject.SetActive(false);
122 buyLayer.gameObject.SetActive(true);
123 }
124
125 pickLayer.updateAnimalIndex();
126 }
127
128 public void updateUI()
129 {
130 upgradeLayer.resetChooseItem();
131 buyLayer.setParams(upgradeInfo.getSpeed(Attr.currentAnimal), upgradeInfo.getJump(Attr.currentAnimal),
132 starCosts[Attr.currentAnimal], goldCosts[Attr.currentAnimal]);
133
134
135
136 }
137
138 public void UpdateCoin()
139 {
140 goldLabel.setText(Data.getData(Data.KEY_COIN) + "", 0, 0);
141 }
142
143 public void buyAnimal2()//test
144 {
145 int gold = Data.getData(Data.KEY_COIN);
146 Data.saveData(Data.KEY_COIN, gold - goldCosts[Attr.currentAnimal]);
147 Data.saveData(Data.KEY_ANIMAL_UNLOCK + Attr.currentAnimal, 1);
148 updateAnimalName();
149 updateUI();
150 upgradeLayer.resetChooseItem();
151 }
152
153 public void buyAnimal()
154 {
155 int gold = Data.getData(Data.KEY_COIN);
156 if (starCosts[Attr.currentAnimal] <= totalStar && goldCosts[Attr.currentAnimal] <= gold)
157 {
158 Data.saveData(Data.KEY_COIN, gold - goldCosts[Attr.currentAnimal]);
159 Data.saveData(Data.KEY_ANIMAL_UNLOCK + Attr.currentAnimal, 1);
160 updateAnimalName();
161 updateUI();
162 upgradeLayer.resetChooseItem();
163 }
164 else
165 {
166 if (goldCosts[Attr.currentAnimal] > gold)
167 {
168 dialog.GetComponent<DialogUnity>().setText("Not enough golds!");
169 dialog.GetComponent<DialogUnity>().showDialog();
170 InputController.Name = InputNames.DIALOG;
171 }
172 else if (starCosts[Attr.currentAnimal] > totalStar)
173 {
174 dialog.GetComponent<DialogUnity>().setText("Not enough stars!");
175 dialog.GetComponent<DialogUnity>().showDialog();
176 InputController.Name = InputNames.DIALOG;
177 }
178 }
179 UpdateCoin();
180 }
181
182 public void showNotEnoughCoin()
183 {
184 dialog.GetComponent<DialogUnity>().setText("Not enough golds!");
185 dialog.GetComponent<DialogUnity>().showDialog();
186 InputController.Name = InputNames.DIALOG;
187 }
188 }
2 using System.Collections;
3 using Dialog;
4
5 public class ShopScreen : InputAdapter {
6
7 public GameObject bgObject;
8 public BitmapFont nameFont;
9
10 public AnimalNameShop animalName;
11 public GameObject lockObject;
12 public GameObject nextButton;
13
14 private string[] animalNames;
15 //private int animalUnlock;
16
17 public PickLayer pickLayer;
18
19 private BitmapFont shopFont;
20 public Label starLabel;
21 public Label goldLabel;
22
23 public UpgradeLayer upgradeLayer;
24 public BuyLayer buyLayer;
25
26 private int[] starCosts;
27 private int[] goldCosts;
28
29 private UpgradeInfo upgradeInfo;
30
31 private int totalStar;
32
33 public GameObject dialog;
34
35 public void Awake()
36 {
37 nameFont = new BitmapFont("Fonts/bitmapfont1", "Fonts/bitmapfont1_xml", gameObject);
38 shopFont = new BitmapFont("Fonts/shop_font","Fonts/shop_font_xml", null);
39 starCosts = new int[] {0, 15, 25, 35, 45, 60, 75, 90, 115, 135 };
40 goldCosts = new int[] {0, 3000, 5000, 7000, 9000, 12000, 15000, 18000, 22000, 26000};
41
42 upgradeInfo = new UpgradeInfo();
43 }
44
45 public void Start()
46 {
47 InputController.Name = InputNames.SHOP;
48
49 bgObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Textures/bg" + (Attr.currentWorld + 1));
50
51 animalNames = new string[] { "dog", "monkey", "pig", "fox", "giraffe", "panda", "rhino", "tiger", "elephant", "lion" };
52 animalName.setFont(nameFont);
53 //animalUnlock = Data.getData(Data.KEY_ANIMAL_UNLOCK);
54
55 updateAnimalName();
56
57 starLabel.setFont(shopFont);
58 totalStar = 0;
59 for (int i = 0; i < 60; i++)
60 {
61 totalStar += Data.getData(Data.KEY_STAR + i);
62 }
63 starLabel.setText("" + totalStar, 0, 0);
64 goldLabel.setFont(shopFont);
65 UpdateCoin();
66
67 upgradeLayer.setFont(shopFont);
68 upgradeLayer.setUpgradeInfo(upgradeInfo);
69 upgradeLayer.UpdateUI(1);
70 upgradeLayer.UpdateUI(0);
71
72 buyLayer.setFont(shopFont);
73 updateUI();
74
75 {
76 dialog = (GameObject)Instantiate(dialog);
77 DialogUnity dialogUnity = dialog.GetComponent<DialogUnity>();
78 //dialogUnity.setText("Do you want to play", "game without skills?");
79 dialogUnity.setDialogOne(delegate() {
80 InputController.Name = InputNames.SHOP;
81 });
82 }
83
84 ARController.setBannerVisible(false);
85 ARController.showInterstitialAd();
86 }
87
88 public void LateUpdate()
89 {
90 if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Backspace))
91 {
92 if (InputController.Name == InputNames.DIALOG)
93 {
94 if (dialog != null)
95 {
96 dialog.GetComponent<DialogUnity>().hideDialog();
97 InputController.Name = InputNames.SHOP;
98 }
99 }
100 }
101 }
102
103 public void updateAnimalName()
104 {
105 animalName.setName(animalNames[Attr.currentAnimal].ToUpper());
106 if (Data.getData(Data.KEY_ANIMAL_UNLOCK + Attr.currentAnimal) == 1)
107 {
108 lockObject.SetActive(false);
109 nextButton.SetActive(true);
110 upgradeLayer.gameObject.SetActive(true);
111
112 upgradeLayer.UpdateUI(1);
113 upgradeLayer.UpdateUI(0);
114
115 buyLayer.gameObject.SetActive(false);
116 }
117 else
118 {
119 lockObject.SetActive(true);
120 nextButton.SetActive(false);
121 upgradeLayer.gameObject.SetActive(false);
122 buyLayer.gameObject.SetActive(true);
123 }
124
125 pickLayer.updateAnimalIndex();
126 }
127
128 public void updateUI()
129 {
130 upgradeLayer.resetChooseItem();
131 buyLayer.setParams(upgradeInfo.getSpeed(Attr.currentAnimal), upgradeInfo.getJump(Attr.currentAnimal),
132 starCosts[Attr.currentAnimal], goldCosts[Attr.currentAnimal]);
133
134
135
136 }
137
138 public void UpdateCoin()
139 {
140 goldLabel.setText(Data.getData(Data.KEY_COIN) + "", 0, 0);
141 }
142
143 public void buyAnimal2()//test
144 {
145 int gold = Data.getData(Data.KEY_COIN);
146 Data.saveData(Data.KEY_COIN, gold - goldCosts[Attr.currentAnimal]);
147 Data.saveData(Data.KEY_ANIMAL_UNLOCK + Attr.currentAnimal, 1);
148 updateAnimalName();
149 updateUI();
150 upgradeLayer.resetChooseItem();
151 }
152
153 public void buyAnimal()
154 {
155 int gold = Data.getData(Data.KEY_COIN);
156 if (starCosts[Attr.currentAnimal] <= totalStar && goldCosts[Attr.currentAnimal] <= gold)
157 {
158 Data.saveData(Data.KEY_COIN, gold - goldCosts[Attr.currentAnimal]);
159 Data.saveData(Data.KEY_ANIMAL_UNLOCK + Attr.currentAnimal, 1);
160 updateAnimalName();
161 updateUI();
162 upgradeLayer.resetChooseItem();
163 }
164 else
165 {
166 if (goldCosts[Attr.currentAnimal] > gold)
167 {
168 dialog.GetComponent<DialogUnity>().setText("Not enough golds!");
169 dialog.GetComponent<DialogUnity>().showDialog();
170 InputController.Name = InputNames.DIALOG;
171 }
172 else if (starCosts[Attr.currentAnimal] > totalStar)
173 {
174 dialog.GetComponent<DialogUnity>().setText("Not enough stars!");
175 dialog.GetComponent<DialogUnity>().showDialog();
176 InputController.Name = InputNames.DIALOG;
177 }
178 }
179 UpdateCoin();
180 }
181
182 public void showNotEnoughCoin()
183 {
184 dialog.GetComponent<DialogUnity>().setText("Not enough golds!");
185 dialog.GetComponent<DialogUnity>().showDialog();
186 InputController.Name = InputNames.DIALOG;
187 }
188 }
private int animalUnlock;
animalUnlock = Data.getData(Data.KEY_ANIMAL_UNLOCK);
dialogUnity.setText("Do you want to play", "game without skills?");
public void buyAnimal2()test